package com.grapes.business;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.grapes.model.Comment;
import com.grapes.model.Reply;
import com.grapes.model.Student;

public class TopicsBusiness {

	private static TopicsBusiness instance;

	private TopicsBusiness() {
	};

	public static TopicsBusiness getInstance() {
		if (null == instance) {
			instance = new TopicsBusiness();
		}
		return instance;
	}

	public List<Comment> getFakedGlobalTopics(Long after) {
		List<Comment> comments = new LinkedList<Comment>();

		return comments;
	}

	public List<Comment> getGlobalTopics(long from, long to,String renrenId) throws JSONException {
		LinkedList<Comment> comments = new LinkedList<Comment>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("renrenId", renrenId);
		params.put("from",
				String.valueOf(from));
		params.put("to",
				String.valueOf(to));
		String result = Connection.getInstance().executeHttpGet(
				Connection.BIZ_GET_COMPANY_TOPIC_LIST, params);
		JSONObject jObj = new JSONObject(result);
		String retCode = jObj.getString("ret_code");
		if (Connection.RESULT_CODE_FAIL == Integer.valueOf(retCode).intValue())
			throw (new JSONException("retCode:"+retCode));
		JSONArray jArray = jObj.getJSONArray("comments");
		for (int index = 0; index < jArray.length(); index++) {
			JSONObject jsonObj = jArray.getJSONObject(index);
			Comment comment = new Comment();
			comment.setCommentId(jsonObj.getString("commentId"));
			comment.setContent(jsonObj.getString("comment_message"));
			comment.setPublishTimeStamp(jsonObj.getString("comment_timestamp"));
			Student publisher = new Student();
			JSONObject publisherOjb = jsonObj.getJSONObject("student");
			publisher.setIconUrl(publisherOjb.getString("renrenIcon"));
			publisher.setStudentName(publisherOjb.getString("renrenUserName"));
			publisher.setSchoolName(publisherOjb.getString("schoolname"));
			comment.setPublisher(publisher);
			List<Reply> replies = new LinkedList<Reply>();
			JSONArray repliesOjb = jsonObj.getJSONArray("replies");
			for (int _index = 0; index < repliesOjb.length(); index++) {
				Reply reply = new Reply();
				JSONObject replyObj = repliesOjb.getJSONObject(_index);
				reply.setAttachedCommentId(comment.getCommentId());
				reply.setReplyId(replyObj.getString("replyId"));
				reply.setReplyTimeStamp(replyObj.getString("reply_timestamp"));
				reply.setReplyContent(replyObj.getString("reply_message"));
				JSONObject replierObj = replyObj.getJSONObject("student");
				Student replier = new Student();
				replier.setIconUrl(replierObj.getString("renrenIcon"));
				replier.setStudentName(replierObj.getString("renrenUserName"));
				replier.setSchoolName(replierObj.getString("schoolname"));
				reply.setReplier(replier);
				replies.add(reply);
			}
			comment.setAttchedReplies(replies);
			comments.add(comment);
		}
		return comments;
	}
	
	public void publishGlobalTopics(String renrenId,String content)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("renrenId", renrenId);
		params.put("content", content);
		params.put("isGlobal", String.valueOf(true));
		Connection.getInstance().executeHttpPost(
				Connection.BIZ_SUBMIT_TOPIC, params);
	}
	
	public void pubTopicReply(String renrenId,String topicId,String replyContent)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("renrenId", renrenId);
		params.put("content", replyContent);
		params.put("commentId", topicId);
		Connection.getInstance().executeHttpPost(
				Connection.BIZ_SUBMIT_REPLAY_FOR_TOPIC, params);
	}
	
}
